from django.shortcuts import render,redirect

from django.views import View
from goods.models import Goods,Brand
from django.http import HttpResponse
# Create your views here.
from django.db.models import Q
def show(request):
    print(request.GET)
    name=request.GET.get('name','')
    price=request.GET.get('price','')
    brand_id=request.GET.get('brand','')
    query=Q()
    if name:
        query=query & Q(name__contains=name)
    if brand_id:
        query=query & Q(brand_id=brand_id)
    if price:
        query = query & Q(price=price)

    data_goods=Goods.objects.filter(query)
    brand = Brand.objects.all()
    get_cookie=request.COOKIES.get('username')
    get_session=request.session.get('id')
    print('session',get_session)
    print('cookie',get_cookie)

    return render(request,'show.html',locals())

def select(request):
    id=request.GET.get('id')
    data_goods=Goods.objects.get(id=id)
    return render(request,'select.html',locals())
def clear(request):
    #删除cookie
    response=HttpResponse("clearcookie")
    response.delete_cookie("username")
    return response

def delete(request):
    id=request.GET.get('id')
    Goods.objects.get(id=id).delete()
    return redirect('/goods/show/')
import datetime
def setcookie(request):
    response=HttpResponse('设置了一个cookie')
    endtime=datetime.datetime.now()+datetime.timedelta(hours=2)
    response.set_cookie('username','xiaobai',expires=endtime)
    return response

def setsession(request):
    request.session['id']=666
    return HttpResponse('设置session')

def delete_session(request):
    if request.session.get('id'):
        del request.session['id']
        return HttpResponse('Session被删了')
    else:
        return HttpResponse('目前没有任何需要删除的session')


class Goods_Add(View):
    def get(self,request):
        brand=Brand.objects.all()
        return render(request,'add.html',locals())
    def post(self,request):
        print(request.POST)
        ret=Goods.objects.create(
            name=request.POST.get('name'),
            price=request.POST.get('price'),
            number=request.POST.get('number'),
            brand_id=request.POST.get('brand')
        )
        if ret:
            return redirect('/goods/show/')
        else:
            return redirect('/goods/add/')


'''
asdasdasd=123
asdasd=123

'''
def select2(request,id):
    data_goods=Goods.objects.get(id=id)
    return render(request,'select.html',locals())




class Goods_Update(View):
    def get(self,request):
        id = request.GET.get('id')

        data_goods=Goods.objects.get(id=id)
        brand_data=Brand.objects.all()

        return render(request,'update.html',locals())
    def post(self,request):
        print(request.POST)
        id = request.GET.get('id')
        ret=Goods.objects.filter(id=id).update(
            name=request.POST.get('name'),
            price=request.POST.get('price'),
            number=request.POST.get('number'),
            brand_id=request.POST.get('brand')
        )
        if ret:
            print('添加成功')

        return redirect('/goods/show/')
